package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SkuService skuService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> queryPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "brandId",required = false)Long brandId,
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "id",required = false)Long id,
            @RequestParam(value = "saleable",required = false)Boolean saleable
    ){
        return ResponseEntity
                .ok(this.spuService.queryPage(page,rows,brandId,categoryId,id,saleable));
    }

    @PostMapping
    public ResponseEntity<Void> saveSpu(@RequestBody SpuDTO spuDTO){
        this.spuService.saveSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id")Long id
            ,@RequestParam("saleable")Boolean saleable){
        spuService.updateSpuSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id")Long id){
        return ResponseEntity.ok(spuService.queryGoodsById(id));

    }

    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long id){
        return ResponseEntity.ok(spuDetailService.querySpuDetailById(id));
    }

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long id){
        return ResponseEntity.ok(skuService.querySkuBySpuId(id));
    }

    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(skuService.querySkuByIds(ids));
    }

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecValues(@RequestParam("id")Long id
            ,@RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(spuDetailService.querySpecValues(id,searching));
    }

    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(spuService.querySpuById(id));
    }

    @PutMapping("/stock/minus")
    public ResponseEntity<Void> reduceSkuStock(@RequestBody Map<Long,Integer> map){
        skuService.reduceSkuStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusSkuStock(@RequestBody Map<Long,Integer> map){
        skuService.plusSkuStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/preview")
    public ResponseEntity<String> querySpuDetailPreview(@RequestParam("id")Long id){
        return ResponseEntity.ok(spuDetailService.querySpuDetailPreview(id));
    }
}
